#include <unistd.h>
#include <string.h>
#include "lm_gb28_man.h"
#include "lm_gb28_sip.h"
#include "lm_media_stream.h"


using namespace std;

void *gb28Man_pth_fun(void* args)
{
	lmGb28Man *gb28Man = (lmGb28Man *)args;
	gb28Man->lm_gb28_client_man_pth_run();
	
	return NULL;
}

void *gb28Client_pth_fun(void* args)
{
	lmGb28Client *gb28Client = (lmGb28Client *)args;
	gb28Client->lm_gbClient_session_proc();
	
	return NULL;
}

void *gb28Client_media_pth_fun(void* args)
{
	lmGb28Client *gb28Client = (lmGb28Client *)args; 
	gb28Client->lm_gbClient_sendRtp_proc();
	
	return NULL;
}

lmGb28Man::lmGb28Man()
{

}

lmGb28Man::~lmGb28Man()
{
	map<uint, devAttr_st *>::iterator it;
	for(it = gbDevMap.begin(); it != gbDevMap.end(); it++)
	{
		if(it->second){
			FREE_OBJ(it->second->dInf);
			FREE_OBJ(it->second);
		}
	}

	map<uint, lmGb28Client *>::iterator it0;
	for(it0 = gbClientMap.begin(); it0 != gbClientMap.end(); it0++)
	{
		if(it0->second){
			FREE_OBJ(it0->second);
		}
	}

	gbClientMap.clear();
	gbDevMap.clear();
}

int lmGb28Man::lm_start_gb28_man()
{
	if(pthread_create(&gb28ManTh, NULL, gb28Man_pth_fun, this) < 0){
		return -1;
	}

	return 0;
}

int lmGb28Man::lm_gb28_client_man_pth_run()
{
	 while(1)
	 {
		sleep(2);
	 }

	return 0;
}

int  lmGb28Man::lm_get_device_from_list(uint serNumb, deviceInfo_st * &devInf)
{
	map<uint, devAttr_st *>::iterator it = gbDevMap.find(serNumb);
	if(it == gbDevMap.end()){
		return -1;
	}

	DO_CONDX(devInf, devInf =  it->second->dInf);

	return 0;
}

int lmGb28Man::lm_device_ctrl(uint     serNumb, deviceCtrl_en ctrlNum)
{

	return 0;
}

int lmGb28Man::lm_device_start(deviceInfo_st *devInf, gb_callback_st *gbcb)
{
	RET_COND(!devInf || !gbcb , -1, "Input devInf  or gbcb is null\n");
	deviceInfo_st *tDevInf =NULL;
	uint devNum = devInf->serial_num, tChnId = devInf->chnId;
	if(lm_get_device_from_list(devNum, tDevInf) == 0){
		return 0;
	}

	RET_COND(tChnId < 0 || tChnId > MAXCHN, -1, "Input chn id error.\n");

	devAttr_st *tmpDevAttr  = new devAttr_st();
	tmpDevAttr->dInf        = new deviceInfo_st();
	
	memset(tmpDevAttr->dInf, 0, sizeof(deviceInfo_st));
	memcpy(tmpDevAttr->dInf, devInf, sizeof(deviceInfo_st));
	tmpDevAttr->gb28Client  = new lmGb28Client(tmpDevAttr->dInf, gbcb);

	deviceInfo_st *p = tmpDevAttr->dInf;
	LOGA("---------222---this[%p],devInf[%p | %p]-->\n", tmpDevAttr->gb28Client, tmpDevAttr->dInf, p);

	pthread_t tdevTh = NULL;
	if(pthread_create(&tdevTh, NULL, gb28Client_pth_fun, tmpDevAttr->gb28Client) < 0){
		FREE_OBJ(tmpDevAttr->dInf);
		FREE_OBJ(tmpDevAttr->gb28Client);
		FREE_OBJ(tmpDevAttr);
		return -1;
	}
	
	tmpDevAttr->gb28ClientTh = &tdevTh;
	tmpDevAttr->runStat = RUN_STAT_CREATE;
	
	gbDevMap[devNum]    = tmpDevAttr;
	gbClientMap[tChnId] = tmpDevAttr->gb28Client;
	
	return 0;
}

int lmGb28Man::lm_device_ctrl_status_get(uint        serNumb, uint &status)
{

	return 0;
}


lmGb28Client::lmGb28Client(deviceInfo_st *dInf, gb_callback_st *gbcb0)
{
	devInf  = dInf;
	gb28Sip = NULL;
	LOGA("---------ttttt--this[%p],devInf[%p]-->\n", this, devInf);

	m_funCb.liveCallFun       = gbcb0->liveCallFun;
	m_funCb.playbackCallFun   = gbcb0->playbackCallFun;
	m_funCb.filedownCallFun   = gbcb0->filedownCallFun;
	
 	playThStat = playBackThStat = downThStat = 0;
	memset(&m_mctl, 0, sizeof(media_ctrl_st));
}

lmGb28Client::~lmGb28Client()
{
	LOGE("---------jhjhjhj--this[%p],devInf[%p]-->\n", this, devInf);

	FREE_OBJ(gb28Sip);
}


int lmGb28Client::lm_gbClient_session_proc()
{
	gb28Sip = new lmGbSip(devInf);
	LOGA("---------t22t2t2t2--this[%p],devInf[%p]-->\n", this, devInf);
	RET_COND(gb28Sip->lm_init_sip() < 0, -1,  "gb28 sip init failed.\n");
	RET_COND(gb28Sip->lm_register_device() < 0, -1, "gb28 register device failed.\n");
	
	gb28Sip->lm_event_process(this);
	
	return 0;
}

int lmGb28Client::lm_gbClient_sendRtp_proc()
{
	lmStreamBase *g_stream = NULL;

	media_type_en mtype = mediaPara.media_ype;

	LOGI("mtype:%d, liveCallFun:%p, playbackCallFun:%p, filedownCallFun:%p\n", mtype, m_funCb.liveCallFun, m_funCb.playbackCallFun, m_funCb.filedownCallFun);
	RET_COND(mtype == TYPE_REALPLAY && !m_funCb.liveCallFun, -1, "funCb->liveCallFun is NULL");
	RET_COND(mtype == TYPE_PLAYBACK && !m_funCb.playbackCallFun, -1, "funCb->playbackCallFun is NULL");
	RET_COND(mtype == TYPE_DOWNLOAD && !m_funCb.filedownCallFun, -1, "funCb->filedownCallFun is NULL");
	
	DO_CONDX(mtype == TYPE_REALPLAY, g_stream = new lmLiveStream(m_funCb.liveCallFun));
	DO_CONDX(mtype == TYPE_PLAYBACK, g_stream = new lmPlaybackStream(m_funCb.playbackCallFun));
	DO_CONDX(mtype == TYPE_DOWNLOAD, g_stream = new lmFileDownStream(m_funCb.filedownCallFun));

	if(g_stream){
		g_stream->lmStreamInit(this, &mediaPara);
		g_stream->lm_start_media_session();
	}
	
	FREE_OBJ(g_stream);
	
	return 0;
}


int lmGb28Client::lm_gbClient_status_get(deviceStat_st *pDeviceStatus)
{

   return 0;
}

int lmGb28Client::lm_device_record_time_get(uint sttime, uint entime, uint itemStime, uint itemEtime)
{

	return 0;
}

int lmGb28Client::lm_gbClient_media_start(media_para_st *p_mediaPara)
{
	mediaPara = *p_mediaPara;
	if(pthread_create(&mediaSessTh, NULL, gb28Client_media_pth_fun, this) < 0){
		PRT_INF("Create media session failed.\n");
		return -1;
	}
	
	return 0;
}

int lmGb28Client::lm_check_device_id(char *inDevid)
{
	RET_COND(!inDevid, -1, "input inDevid is null\n");
	
	LOGA("---------444444--this[%p],devInf[%p]-->\n", this, devInf);
	
	/*LOGA("---------323232--[%s][%s]--->\n", devInf->ipc_id, inDevid);
	if(strncmp((const char*)devInf->ipc_id, inDevid,  strlen(inDevid))){
		return -1;
	}*/
	
	return 0;
}


int lmGb28Client::lm_gb28_cmd_proc(char *cmdType)
{


	return 0;
}


int lmGb28Client::lm_printf_test(int ddd)
{

	LOGA("--tttt---[%d]--this[%p],devInf[%p]-->\n", ddd,this, devInf);

	return 0;
}




